package com.gp.service.impl;

import com.gp.dao.TbCarTypeDao;
import com.gp.service.CarTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gp.entity.TbCarType;
import com.gp.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class CarTypeServiceImpl implements CarTypeService {

    @Autowired(required = false)
    private TbCarTypeDao tbCarTypeDao;

    @Override
    public CommonResult findAllType(Integer page, Integer limit, String typename) {
        QueryWrapper<TbCarType> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(typename)){
            wrapper.like("type_name",typename);
        }
        Page<TbCarType> page1=new Page<>(page,limit);
        Page<TbCarType> page2 = tbCarTypeDao.selectPage(page1, wrapper);
        return new CommonResult(200,"查询成功",page2);
    }

    @Override
    public CommonResult delTypeById(Integer id) {
        int i = tbCarTypeDao.deleteById(id);
        if (i > 0) {
            return new CommonResult(200, "删除成功", null);
        } else {
            return new CommonResult(500, "删除失败", null);
        }
    }

    @Override
    public CommonResult insert(TbCarType tbCarType) {
        tbCarType.setAddtime(new Date());
        int insert = tbCarTypeDao.insert(tbCarType);
        if (insert > 0) {
            return new CommonResult(200, "添加成功", null);
        } else {
            return new CommonResult(500, "添加失败", null);
        }
    }

    @Override
    public CommonResult updateCarType(TbCarType tbCarType) {
        int i = tbCarTypeDao.updateById(tbCarType);
        if (i> 0) {
            return new CommonResult(200, "修改成功", null);
        } else {
            return new CommonResult(500, "修改失败", null);
        }
    }
}
